package com.iafme.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.ProcessReqDTO;
import com.iafme.dto.res.ProcessResDTO;
import com.iafme.model.Process;
import com.iafme.service.ProcessService;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @version 1.0
 * @date 2025/2/21 21:03
 */
@RestController
@RequestMapping("/process")
@AllArgsConstructor
public class ProcessController implements BaseCommonInterface<ProcessReqDTO, ProcessResDTO> {

    private final ProcessService processService;

    @OperateAnnotation(businessType = BusinessType.INSERT,name = "发展历程")
    @SaCheckPermission("world:process:add")
    @Override
    public Result<String> save(@Valid ProcessReqDTO processReqDTO) {
        Process process = Convert.convert(Process.class, processReqDTO);
        processService.save(process);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "发展历程")
    @SaCheckPermission("world:process:edit")
    @Override
    public Result<String> update(@Valid ProcessReqDTO processReqDTO) {
        Process process = Convert.convert(Process.class, processReqDTO);
        processService.updateById(process);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.DELETE,name = "发展历程")
    @SaCheckPermission("world:process:delete")
    @Override
    public Result<String> del(Integer id) {
        processService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<ProcessResDTO>> lists(ProcessReqDTO processReqDTO) {
        return Result.successResult(processService.lists(processReqDTO));
    }

    @Override
    public Result<ProcessResDTO> findById(Integer id) {
        processService.removeById(id);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "发展历程")
    @SaCheckPermission("world:process:delete")
    @Override
    public Result<String> batchDel(List<ProcessResDTO> list) {
        List<Integer> ids = list.stream().map(ProcessResDTO::getId).collect(Collectors.toList());
        processService.removeByIds(ids);
        return Result.successResult();
    }
}
